home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6598 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: news.one.net!mcfarlan
  2. From: rcm@one.net (Ronald C. McFarland)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Watch out! C "gotcha!"
  5. Date: Thu, 15 Feb 1996 02:13:01 GMT
  6. Organization: The PC running Linux on the desk over there
  7. Message-ID: <1996Feb15.021301.2697@cannon.interramp.com>
  8. References: <4fthhh$7th@blackice.winternet.com>
  9. NNTP-Posting-Host: port-7-2.access.one.net
  10.  
  11. mschwarz@winternet.com (Michael Schwarz) is astonished to find that:
  12.  
  13. > [plain 'ol C labels can be embedded (and used) inside of switch statements]
  14. >
  15. >Does anyone know of:
  16. >
  17. > [snip]
  18. >
  19. >3)    The ANSI party line on labels -- is there an ANSI enforcement option
  20. >    that guarantees a warning on a construct like this?  (If not, does
  21. >    anyone know how to suggest revisions to the X3-J11 standard?)
  22.  
  23. Labels embedded inside the body of "switch" statements are perfectly legal.
  24. This has always been true and, if you think about it, is no different than
  25. embedding labels inside the bodies of "for" statements, "while" statements,
  26. or what-have-you statements.
  27.  
  28. The standard doesn't specify when or where compilers must give *warnings*.
  29. In fact, there is no such concept in the standard.  The standard says that
  30. the compiler *must* issue diagnostics when presented with certain violations.
  31. Also, it states that "no diagnostic is required" when presented with others.
  32.  
  33. It also says that compilers are free to issue any diagnostics that they wish
  34. at any time, as long as they correctly translate conforming code.  So, it
  35. is left completely up to the compiler writer whether or not it offers a way
  36. to flag possible mistakes like the one you have described.
  37.  
  38. ObSillyImplication (1): Given that the standard does not specify what exact
  39. form diagnostics must take, a conforming compiler could choose to issue its
  40. diagnostics by flashing the "Caps Lock" LED on your keyboard whenever it
  41. encounters a syntax error.
  42.  
  43. ObSillyImplication (2): The wording in the standard w.r.t. diagnostics
  44. can be summarized as "sometimes the compiler *must* issue a diagnostic and
  45. it *can* issue diagnostics at any time."  So, a conforming compiler could
  46. be written which always just prints out "Your program might contain one
  47. or more errors" whenever it gets run.  The standard requires nothing more
  48. than that.
  49.  
  50. >The thing that scares me the most about this is that either I've never before
  51. >made this particular mistake or for the last ten years I've been leaving
  52. >behind a number of ticking code bombs...
  53.  
  54. This scares you so much because you happened to get bitten by it.  But if
  55. you think about it, this mistake is no worse than many others.  Finding
  56. a bug caused by a mis-spelled "default" label is probably easier than
  57. finding one where the programmer typed "+" when he meant to type "-".
  58.  
  59. If nothing else, under Un*x at least, one can
  60.  
  61.     foo% grep "d.*:" *.c
  62.  
  63. as a quick check for mis-spelled "default" labels.
  64.  
  65. -- 
  66. Ron McFarland
  67. rcm@one.net
  68.